﻿package com.toncen.samepms.net.dao;

import java.util.List;

import com.toncen.samepms.net.domain.ChanelInformation;
import com.toncen.samepms.net.domain.Information;
import com.toncen.samepms.common.AbstractDao;
import com.toncen.samepms.common.IDomain;

/**
 * 描述：信息表NET_INFORMATION的dao文件
 * 
 * @time 2013-5-10
 * @author xyp
 */
public class InformationDao extends AbstractDao<Information> {

	public InformationDao() {
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.comit.common.db.BaseDao#initialize()
	 */
	@Override
	protected void initialize() {
		setStatementPrefix("Information");
		setStatementDivision(".");
	}

	/**
	 * 级联新增信息栏目表
	 * 
	 * @param exclude
	 * @return
	 * @throws Exception
	 */
	@Override
	public long add(Information domain) throws Exception {
		long id = super.add(domain);
		ChanelInformationDao chanelInformationDao = new ChanelInformationDao();
		for (ChanelInformation chanelInformation : domain.getChanelInformation()) { // 重新设置关联
			chanelInformation.setId(IDomain.UNSAVED_ID);
			chanelInformation.setInformation(domain);
		}
		chanelInformationDao.add(domain.getChanelInformation());
		ChannelDao channelDao = new ChannelDao();
		channelDao.updateLastWriteTime(); // 更新栏目最后修改时间
		return id;
	}

	/*
	 * (non-Javadoc) 级联修改信息栏目表
	 * 
	 * @see com.toncen.samepms.common.AbstractDao#update(com.toncen.samepms.common.IDomain)
	 */
	@Override
	public int update(Information domain) throws Exception {
		for (ChanelInformation chanelInformation : domain.getChanelInformation()) {// 防止延迟加载没有加载就删除了
			chanelInformation.getInformation();
		}

		ChanelInformationDao chanelInformationDao = new ChanelInformationDao();
		Information oldDomain = find(domain);
		if (oldDomain != null) {
			chanelInformationDao.deleteByInformationId(domain.getId());
		}
		chanelInformationDao.add(domain.getChanelInformation());
		int result = super.update(domain);
		ChannelDao channelDao = new ChannelDao();
		channelDao.updateLastWriteTime(); // 更新栏目最后修改时间
		return result;
	}

	/*
	 * (non-Javadoc) 级联删除信息栏目表
	 * 
	 * @see com.toncen.samepms.common.AbstractDao#delete(long)
	 */
	@Override
	public int delete(long id) throws Exception {
		ChanelInformationDao chanelInformationDao = new ChanelInformationDao();
		chanelInformationDao.deleteByInformationId(id);
		int result = super.delete(id);
		ChannelDao channelDao = new ChannelDao();
		channelDao.updateLastWriteTime(); // 更新栏目最后修改时间
		return result;

	}

	/**
	 * 根据栏目id和显示条数查找图片信息
	 * 
	 * @param chanelId
	 * @param pagesize
	 * @return
	 * @throws Exception
	 */
	@SuppressWarnings("unchecked")
	public List<Information> getImageByChannelId(long chanelId, int pagesize) throws Exception {
		return getSqlMap().queryForList(getStatement("getImageByChannelId"), chanelId, 0, pagesize);
	}

	/**
	 * 根据栏目id和显示条数查找信息
	 * 
	 * @param chanelId
	 * @return
	 * @throws Exception
	 */
	@SuppressWarnings("unchecked")
	public List<Information> getInformationByChannelId(long chanelId, int pagesize) throws Exception {
		return getSqlMap().queryForList(getStatement("getInformationByChannelId"), chanelId, 0, pagesize);
	}
}
